package ahxu.commons.fileupload;


import org.apache.commons.fileupload.ProgressListener;

public abstract class AbstractAhxuProgressListener implements ProgressListener {
	private long reportHoldSize=1024*1000;
	private long startTime=0;
	private ReportItem reportItem;
	
	public  AbstractAhxuProgressListener(ReportItem item){
		this.reportItem=item;
	}

	public void update(long pBytesRead, long pContentLength, int pItems) {
		if(startTime==0) startTime=System.currentTimeMillis();
		if(pBytesRead!=0 && pBytesRead%reportHoldSize==0){
			long nowTime=System.currentTimeMillis();
			if(nowTime==startTime) return;
			this.reportItem.setStartTime(this.startTime);
			this.reportItem.setUploadTime(nowTime);
			this.reportItem.setTotalSize(pContentLength);
			this.reportItem.setUploadSize(pBytesRead);
			this.reportItem.setUploadedFileSum(pItems);
			this.updateDo(this.reportItem);
			

		}

	}
	
	abstract public void updateDo(ReportItem reportItem);
	
	public long getReportHoldSize() {
		return reportHoldSize;
	}

	public void setReportHoldSize(long reportHoldSize) {
		this.reportHoldSize = reportHoldSize;
	}
	
	//private 

}
